/*******************************************************************************
filename      StringCommentPrinter.RegEx.Greedy.cpp
author        Craig Williams

Brief Description:
  Prints all strings, C Style comments, and C++ Style comments from a file.
    If a file is not specified, Parser.c will be processed.

  RegEx version of StringCommentPrinter. This version does not have multiple
    token sets. Instead, all the logic is handled by the actual RegEx.

  Note: This version is significantly slower then StringCommentPrinter.c
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>

#include "Parser.h"

char *PrintString (Parser &p, char *sp, void *);
char *PrintCComment(Parser &p, char *comment, void *unused);
char *PrintCPPComment(Parser &p, char *comment, void *unused);

int main(int argc, char *argv[])
{
  char  *buffer;
  Parser p(0);

    /* Initalize the parser */
  if(argc == 1)
    p.LoadFile("Parser.cpp"); /* No file specified. Assume Parser.cpp       */
  else
    p.LoadFile(argv[1]);    /* Attempt to load what ever is specified   */

  p.Enable(PARSER_REGULAR_EXPRESSIONS);

  if(p.AddTokenSeparator("//[^\n\r]*", 0, -1, 0, PrintCPPComment, 0))
    printf("Compile Error: CPP Style Comment\n");

  if(p.AddTokenSeparator("/\\*([^*]|\\*+[^*/])*\\*+/", 0, -1, 0, PrintCComment, 0))
    printf("Compile Error: C Style Comment\n");

  if(p.AddTokenSeparator("\"([^\"\\]|\\\\.)*\"", 0, -1, 0, PrintString, 0))
    printf("Compile Error: String\n");

  for(buffer = p.GrabToken(); buffer; buffer = p.GrabToken())
    free(buffer);

  return 0;
}

char *PrintString(Parser &p, char *sp, void *unused)
{
  printf("String Found!\n%s\n\n", sp);

  return sp;  /* Don't return the " */
}

char *PrintCComment(Parser &p, char *comment, void *unused)
{
  printf("C Style Comment--------------------\n");
  printf("%s\n\n", comment);
  return comment;
}

char *PrintCPPComment(Parser &p, char *comment, void *unused)
{
  printf("C++ Style Comment------------------\n");
  printf("%s\n\n", comment);
  return comment;
}

